package com.fayuan.nocv.TXApi;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fayuan.nocv.entity.CiYun;
import com.fayuan.nocv.service.CiYunService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Component
@Slf4j
public class HotNews {
    @Autowired
    private CiYunService ciYunService;

    @Scheduled(fixedDelay = 3600000)
    public void CiYunSave() throws IOException {

        HttpUtils2 httpUtils2 = new HttpUtils2();

            String s = httpUtils2.getData();
            JSONArray jsonObject = JSONObject.parseArray(s);
            Object o = jsonObject.get(0);
            JSONObject jsonObject1 = JSONObject.parseObject(o.toString());
        JSONArray data = jsonObject1.getJSONArray("data");
            List<CiYun>ciYunList = new ArrayList<>();
        for (Object datum : data) {
            JSONObject jsonObject2 = JSONObject.parseObject(datum.toString());
            CiYun ciYun = new CiYun();
            Object word = jsonObject2.get("word");
//            Object realpos = jsonObject2.get("realpos");
            Object num = jsonObject2.get("num");

            ciYun.setName(word.toString());
            ciYun.setValue(Integer.parseInt(num.toString()));
            ciYunList.add(ciYun);
        }
        ciYunService.remove(new QueryWrapper<>());
        boolean b = ciYunService.saveBatch(ciYunList);
        if (b){
            log.info("热搜插入成功！");
        }
    }
}
